package com.example.mydemo103.ui.borrowbook;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.mydemo103.R;
import com.example.mydemo103.bean.Book;
import com.example.mydemo103.util.Config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class borrowFragment extends Fragment {

    private GridView gview;
    private List<Map<String, Object>> data_list;
    private SimpleAdapter sim_adapter;
    private Book showbook;
    // 图片封装为一个数组
    private Integer[] icon = { R.drawable.book3, R.drawable.book8,
            R.drawable.book16};
    ArrayList<Integer> iconList = new ArrayList<>();
    private String[] iconName = { "第一本书", "第二本书", "第三本书" };
    ArrayList<String> iconNameList = new ArrayList<>();


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_borrow,container,false);
        showbook= Config.bookbrow;
        if (showbook!=null){
            brow();
        }
//        String bookimg= getArguments().getString("bookimg");
//        if (getArguments().getString("bookimg").equals("")) {//判断Fragment已经依附Activity

//        }else {}
//        Log.e("传输过来的值：",bookimg);
//        Bundle bundle =this.getArguments();
//        if(bundle!=null){
//            String bookimg= bundle.getString("bookimg");
//            Log.e("传输过来的值：",bookimg);}
//        else {
//            Log.e("传输过来的值：","是空的");
//        }
        gview =view.findViewById(R.id.girdview);
        data_list = new ArrayList<Map<String, Object>>();
        getData();
        String [] from ={"image","text"};
        int [] to = {R.id.gridimg,R.id.gridtext};
        sim_adapter = new SimpleAdapter(getActivity(), data_list, R.layout.gridview_layout, from, to);
        gview.setAdapter(sim_adapter);
        return view;
    }
    public List<Map<String, Object>> getData() {
        //cion和iconName的长度是相同的，这里任选其一都可以
        for (int i = 0; i < icon.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            data_list.add(map);
        }
        return data_list;
    }
    public void brow(){
    showbook= Config.bookbrow;
    int bookid=showbook.getBookimg();
        if (bookid==1){
        iconNameList.add("第一本书");
        iconNameList.add("第二本书");
        iconNameList.add("第三本书");
        iconNameList.add("第四本书");
        iconList.add(R.drawable.book3);
        iconList.add(R.drawable.book8);
        iconList.add(R.drawable.book16);
        iconList.add(R.drawable.book10);
    }
    iconName = iconNameList.toArray(new  String[iconNameList.size()]);
    icon = iconList.toArray(new  Integer[iconList.size()]);
    }
}